<template>
	<view class="route-address">
		<view class="content">
			<view class="have" v-if="addressList.length">
				<view class="cloum" v-for="(item,index) in addressList" :key="index" @click="handleSelect(item)">
					<view class="cloum-left">
						<view class="nameandphone">
							<view class="name">
								{{item.name}}
							</view>
							<view class="phone">
								{{item.phone}}
							</view>
							<!-- <view class="default">
								默认
							</view> -->
						</view>
						<view class="desc">
							{{item.area}}{{item.menpai}}
						</view>
					</view>
					<view class="cloum-right" v-if="!isSelect">
						<view class="iconcont" @click="handleEdit(item)">
							<u-icon name="edit-pen" color="#ED2920" size="16"></u-icon>
						</view>
						<view class="iconcont" @click="handleDel(item)">
							<u-icon name="trash" color="#ED2920" size="16"></u-icon>
						</view>
					</view>
				</view>
			</view>
			<view class="none" v-else>
				<u-empty mode="address" text="暂无地址"></u-empty>
			</view>

		</view>
		<view class="footer">
			<view class="btn" @click="handleGoEditAddress">
				新建地址
			</view>
		</view>
	</view>
</template>

<script setup>
	import handleCache from "@/utils/cache.js"
	import {

		addressApi
	} from '../../api';
	import {
		ref
	} from 'vue'
	import {
		onReady,
		onShow,
		onLoad
	} from '@dcloudio/uni-app'
	const addressList = ref([])
	const isSelect = ref(false)
	const userInfo = ref('')
	onShow(() => {
		userInfo.value = handleCache.get('userInfo')
		getAddress()
	})
	onLoad((options) => {
		if (options.type) {
			isSelect.value = true
			uni.setNavigationBarTitle({
				title: '选择地址'
			})
		}
	})
	const handleGoEditAddress = () => {
		uni.navigateTo({
			url: `/pages/editaddress/editaddress?type=${isSelect.value?'select':''}`
		})
	}

	const getAddress = async () => {
		let {
			data
		} = await addressApi.addrlist({
			uid: userInfo.value.id
		})
		addressList.value = data || []

	}
	const handleSelect = (data) => {
		if (!isSelect.value) {
			return
		}

		let pages = getCurrentPages(); //获取所有页面栈实例列表
		let nowPage = pages[pages.length - 1]; //当前页页面实例
		let prevPage = pages[pages.length - 2]; //上一页页面实例

		prevPage.$vm.$.exposed.setSelectAddress(data)
		uni.navigateBack(-1)
	}
	const handleEdit = (data) => {
		uni.navigateTo({
			url: '/pages/editaddress/editaddress?id=' + data.id
		})
	}
	const handleDel = (data) => {
		uni.showModal({
			title: '提示', // 标题
			content: '确定删除当前地址吗?', // 提示内容
			labelColor: '#3385e3', // 确认按钮文字颜色
			cancelText: "取消", // 取消按钮的文字
			confirmText: "确认", // 确认按钮的文字
			showCancel: true, // 是否显示取消按钮，默认为 true
			confirmColor: '#ED2920',

			success(res) {
				if (res.confirm) {
					//点击确定之后执行的代码
					addressApi.addrdel({
						id: data.id
					}, {
						loading: {
							text: '删除中'
						}
					}).then((res) => {
						getAddress()
					})
				} else {
					//点击取消之后执行的代码
				}
			}
		});
	}
</script>

<style lang="scss" scoped>
	@import 'index.scss'
</style>